package com.christian.thread.createway;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 文件说明：使用 Callable Future 模式创建线程
 *
 * @Author wang
 * @Date 2020/1/15 9:25
 */
public class CreateThread04 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Long startTime = System.currentTimeMillis();
        // 创建FutureTask对象
        FutureTask<String> futureTask = new FutureTask<String>(() -> {
            try {
                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return Thread.currentThread().getName() + "：正在执行";
        });

        //创建并启动线程
        new Thread(futureTask, "Thread1").start();

        //打印返回值
        System.out.println("返回结果：" + futureTask.get());

        Long endTime = System.currentTimeMillis();

        System.out.println("耗时：" + ((endTime - startTime) / 1000) + "s");
    }
}
